package gestionedt.models;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 *
 */
@Entity
@Table(name = "prestation")
@NamedQueries({@NamedQuery(name = "Prestation.findAll",
                           query = "SELECT p FROM Prestation p"),
               @NamedQuery(name = "Prestation.findById",
                           query = "SELECT p FROM Prestation p WHERE p.id = :id")})
public class Prestation implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @JoinTable(name = "groupes_par_prestation", joinColumns = {@JoinColumn(name = "idPrestation", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "idGroupe", referencedColumnName = "id")})
    @ManyToMany
    private Collection<Groupe> groupeCollection;
    @JoinColumn(name = "idEnseignant", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Enseignant idEnseignant;
    @JoinColumn(name = "idModule", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Module idModule;
    @JoinColumn(name = "numCreneau", referencedColumnName = "numero")
    @ManyToOne
    private Creneau numCreneau;

    public Prestation() {
    }

    public Prestation(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Collection<Groupe> getGroupes() {
        return groupeCollection;
    }

    public void setGroupes(Collection<Groupe> groupeCollection) {
        this.groupeCollection = groupeCollection;
    }

    public Enseignant getEnseignant() {
        return idEnseignant;
    }

    public void setEnseignant(Enseignant enseignant) {
        this.idEnseignant = enseignant;
    }

    public Module getModule() {
        return idModule;
    }

    public void setModule(Module module) {
        this.idModule = module;
    }

    public Creneau getCreneau() {
        return numCreneau;
    }

    public void setCreneau(Creneau numCreneau) {
        this.numCreneau = numCreneau;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Prestation)) {
            return false;
        }
        Prestation other = (Prestation) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "prestation id=" + getId();
    }

    public int getEffectif() {
        int effectif = 0;
        for(Groupe groupe : getGroupes())
            effectif += groupe.getEffectif();
        return effectif;
    }

}
